<?php
/**
 * Atlas KOTASOFT cakephp
 *
 * Bakes web interface 
 *
 * PHP versions 4 and 5
 *
 * ATLAS KOTASOFT  cakephp web code generator
 * Copyright 2009, Kotasoft agile factory, Inc. (http://www.atlascakephp.com)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009, Kotasoft agile factory , Inc. (http://www.kotasoft.com)
 * @link          http://www.atlascakephp.com  ATLAS KOTASOFT Project
 * @version       BETA 0.5 $
 * @modifiedby    $LastChangedBy: daniel castanera - guldoe and francisco dominguez - idioto $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

define ('ROUTE_GENERATOR',"kotasoft");
define ('KT_SESSION','kt_config');
define ('KT_USERDBCONFIG','atlas_conf');
define ('KT_CONFIGURATION','Ktconfiguration');
define ('KT_NAME','name');
define ('KT_DIRECTORY','directory');
define ('KT_HOST','hosts');
define ('KT_DATABASE','database');
define ('KT_LOGIN','login');
define ('KT_PASS','password');

class AppController extends Controller
{
 	var $helpers = array('Html', 'Form', 'Javascript'); 
 	var $uses = array('Role', 'User');
 	
 	var $components = array('RequestHandler');  
 		
 	/* check admin session */
	function checkAdminSession(){        
        if (!$this->Session->check('User') && isset($this->params['prefix'])) {            
            $this->redirect('/users/login');
            exit();
        }
    }
    
    /* check configuration */
    function checkConfiguration(){
           $session=$this->Session->read(KT_SESSION); 
		   if ($session==null){
				$this->Session->setFlash(__('First, you must select configuration',true));	
				$this->redirect('/admin/ktconfigurations/');	
				exit();		
		    }
    } 
 
    /*Creates the proper controller camelized name (singularized) for the specified name*/
	function _modelName($name) {
		return Inflector::camelize(Inflector::singularize($name));
	}
	
    /* creates the singular name for  */
	function _singularName($name) {
		return Inflector::variable(Inflector::singularize($name));
	}
	
	/* Creates the plural name  */
	function _pluralName($name) {
		return Inflector::variable(Inflector::pluralize($name));
	}
	
	/* Creates the singular human name u */
	function _singularHumanName($name) {
		return Inflector::humanize(Inflector::underscore(Inflector::singularize($name)));
	}
	
	/* Creates the plural human name*/
	function _pluralHumanName($name) {
		return Inflector::humanize(Inflector::underscore(Inflector::pluralize($name)));
	}

	
	
	/* function create file */
	function createFile ($path, $contents) {		
		if (is_file($path)) {
		    // TODO que ocurre cuando ya existe el fichero, mostramos mensaje de error?
		}
		if (!class_exists('File')) {
			uses('file');
		}

		if ($File = new File($path, true)) {
			$data = $File->prepare($contents);
			$File->write($data);			
			return true;
		} else {			
			return false;
		}
	}
	
	/* check directory configuration */
	function __checkDirectory() {
		$session=$this->Session->read(KT_SESSION); 			
		$directorio=$session['Ktconfiguration']["directory"];
		$mensaje="";
		if (!file_exists($directorio)) {
			$mensaje = __('No exists directory.',true);
			$mensaje .= __('Error create file.',true);
		}
	    if (!file_exists($directorio.DS."app")) {
			$mensaje .= __('No exists directory app.',true);
		} 
		
		if ($mensaje !="") {
			$this->Session->setFlash($mensaje);	
		}		
	}	
	
	function __checkDirectoryProject(){
		$session=$this->Session->read(KT_SESSION); 			
		$directorio=$session['Ktconfiguration']["directory"];
		if (!file_exists($directorio)) {
			return false;
		}
		return true;
	}
	
	/* before render checks user */
	function beforeRender() {
	  if (!$this->Session->check('User')) {
            // Force the user to login  
            $this->set('checkUser', false);            
        } else {   
        	$this->set('checkUser', true); 
        	$user= $this->Session->read('User'); 	
        	$this->set('userName',$user['username']);          	
        }		
	}
	
	
	
	/* connections */
	function __dbConfig() {
		$this->useDbConfig = 'default';		
		$configs = get_class_vars('DATABASE_CONFIG');
				
		if (!is_array($configs)) {
			return $this->DbConfig->execute();
		}		

		$connections = array_keys($configs);
		if (count($connections) > 1) {
			 $array= Array();
			 foreach ($connections as $indice => $value){
				 $array[$value]=$value;			
			 }  
			$this->set('dbConections', $array);			
			return true;	
		}
	}
	
}
?>